package com.ydm.myapp001.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ydm.myapp001.R;

import org.w3c.dom.Text;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by yangdongming on 2017/11/28.
 */

public class SecondActivity extends AppCompatActivity {
    private Button btn1;
    private Button btn2;
    private EditText editText;
    private Button writeAndReadBtn;
    private TextView contentValue;
    private static final String TAG = "YDMLog_SecondActivity";
    private Context mContext;

    private static final String GPS_LOCATION_NAME = LocationManager.GPS_PROVIDER;
    private static final int REQUEST_PRESSMION_CODE = 10000;
    private LocationManager locationManager;
    private boolean isGpsEnabled;
    private String locateType;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        mContext = this;

        /***** btn1 ****/
        btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("key1", "你好，我是杨冬明");
                setResult(11, intent);
                finish();
            }
        });

        /***** btn2---SharedPreferences使用****/
        btn2 = (Button) findViewById(R.id.button2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences pref = getSharedPreferences("myPref", MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                editor.putString("name", "张三");
                editor.putInt("age", 30);
                editor.putLong("time", System.currentTimeMillis());
                editor.putBoolean("default", true);
                editor.commit();
                editor.remove("default");
                editor.commit();
                Log.d(TAG, "取出存储的name：" + pref.getString("name", "未知"));
                Log.d(TAG, "取出存储的age：" + pref.getInt("age", -1));
                Log.d(TAG, "取出存储的birthday：" + pref.getString("birthday", "未知"));
            }
        });



        /**********文件写与读*********/
        editText = (EditText) findViewById(R.id.editText1);
        writeAndReadBtn = (Button) findViewById(R.id.write);
        contentValue = (TextView) findViewById(R.id.contentvalue);
        writeAndReadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WriteFiles(editText.getText().toString());
                contentValue.setText(readFiles());
            }
        });
    }

    /*用xml文件创建菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.second, menu);

        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.action_menu_item1:
                Toast.makeText(this,"点击了菜单一",Toast.LENGTH_SHORT);
                break;
            case R.id.action_menu_item2:
                Toast.makeText(this,"点击了菜单二",Toast.LENGTH_SHORT);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    */


    /**
     * 动态添加菜单项
     **/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(1, 101, 1, "菜单一");
        menu.add(1, 102, 1, "菜单二");
        menu.add(1, 103, 1, "菜单三");
        menu.add(1, 104, 1, "菜单四");
        menu.add(1, 105, 1, "菜单五");
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 101:
                Toast.makeText(this, "点击了菜单一", Toast.LENGTH_SHORT);
                break;
            case 102:
                Toast.makeText(this, "点击了菜单二", Toast.LENGTH_SHORT);
                break;
            case 103:
                Toast.makeText(this, "点击了菜单三", Toast.LENGTH_SHORT);
                break;
            case 104:
                Toast.makeText(this, "点击了菜单四", Toast.LENGTH_SHORT);
                break;
            case 105:
                Toast.makeText(this, "点击了菜单五", Toast.LENGTH_SHORT);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    //保存文件内容
    public void WriteFiles(String content) {
        try {
            FileOutputStream fos = openFileOutput("a.txt", MODE_PRIVATE);
            fos.write(content.getBytes());
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //读取文件内容
    public String readFiles(){
        String content = null;

        try {
            FileInputStream fis = openFileInput("a.txt");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;

            while ((len=fis.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            content = baos.toString();
            fis.close();
            baos.close();
        } catch(FileNotFoundException e) {
            e.printStackTrace();;
        } catch (IOException e) {
            e.printStackTrace();
        }



        return content;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: SecondActivity");
    }
}

